//////////////////////////////////////////////////////////////////////////////////
//
//	DoD Explosives
//		- Beta Version 1
//		- 09.11.2006
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
//	- Place timed tnt & satchel charges
//
// Things To Do:
//
//	- Improve apon placing of charges
//	- Allow specific or multiple delay times
//	- Have them trigger func_breakables & standard dod bomb targets
//	- Rework how damage is done
//
// CVARs:
//
//	dod_explosives "1"		//Turn ON(1)/OFF(0)
//	dod_explosives_tntdelay "5"
//	dod_explosives_satcheldelay "5"
//	
//	//these set how many each class spawns with:
//	dod_explosives_tnt_garand "1"
//	dod_explosives_tnt_carbine "0"
//	dod_explosives_tnt_thompson "0"
//	dod_explosives_tnt_grease "0"
//	dod_explosives_tnt_sniper "0"
//	dod_explosives_tnt_bar "0"
//	dod_explosives_tnt_30cal "0"
//	dod_explosives_tnt_bazooka "2"
//	dod_explosives_satchel_garand "1"
//	dod_explosives_satchel_carbine "0"
//	dod_explosives_satchel_thompson "0"
//	dod_explosives_satchel_grease "0"
//	dod_explosives_satchel_sniper "0"
//	dod_explosives_satchel_bar "0"
//	dod_explosives_satchel_30cal "0"
//	dod_explosives_satchel_bazooka "3"
//	dod_explosives_tnt_enfield "1"
//	dod_explosives_tnt_sten "0"
//	dod_explosives_tnt_marksman "0"
//	dod_explosives_tnt_bren "0"
//	dod_explosives_tnt_piat "2"
//	dod_explosives_satchel_enfield "1"
//	dod_explosives_satchel_sten "0"
//	dod_explosives_satchel_marksman "0"
//	dod_explosives_satchel_bren "0"
//	dod_explosives_satchel_piat "3"
//	dod_explosives_tnt_kar "1"
//	dod_explosives_tnt_k34 "0"
//	dod_explosives_tnt_mp40 "0"
//	dod_explosives_tnt_mp44 "0"
//	dod_explosives_tnt_scharfschutze "0"
//	dod_explosives_tnt_fg42 "0"
//	dod_explosives_tnt_fg42scoped "0"
//	dod_explosives_tnt_mg34 "0"
//	dod_explosives_tnt_mg42 "0"
//	dod_explosives_tnt_panzerjager "2"
//	dod_explosives_satchel_kar "1"
//	dod_explosives_satchel_k34 "0"
//	dod_explosives_satchel_mp40 "0"
//	dod_explosives_satchel_mp44 "0"
//	dod_explosives_satchel_scharfschutze "0"
//	dod_explosives_satchel_fg42 "0"
//	dod_explosives_satchel_fg42scoped "0"
//	dod_explosives_satchel_mg34 "0"
//	dod_explosives_satchel_mg42 "0"
//	dod_explosives_satchel_panzerjager "3"
//
// Console Commands:
//
//	plant_tnt	//place a tnt charge
//	plant_satchel	//place a satchel charge
//
// Changelog:
//
//	- 09.11.2006 Beta Version 1
//		Initial test version
//
///////////////////////////////////////////////////////////////////////////////////////

#include <amxmodx>
#include <fakemeta>
#include <dodfun>
#include <dodx>

new p_plugin, p_tntdelay, p_satcheldelay
new p_tnt_garand, p_tnt_carbine, p_tnt_thompson, p_tnt_grease, p_tnt_sniper, p_tnt_bar, p_tnt_30cal, p_tnt_bazooka
new p_satchel_garand, p_satchel_carbine, p_satchel_thompson, p_satchel_grease, p_satchel_sniper, p_satchel_bar, p_satchel_30cal, p_satchel_bazooka
new p_tnt_enfield, p_tnt_sten, p_tnt_marksman, p_tnt_bren, p_tnt_piat
new p_satchel_enfield, p_satchel_sten, p_satchel_marksman, p_satchel_bren, p_satchel_piat
new p_tnt_kar, p_tnt_k43, p_tnt_mp44, p_tnt_mp40, p_tnt_scharfschutze, p_tnt_fg42, p_tnt_scoped_fg42, p_tnt_mg34, p_tnt_mg42, p_tnt_panzerjager
new p_satchel_kar, p_satchel_k43, p_satchel_mp44, p_satchel_mp40, p_satchel_scharfschutze, p_satchel_fg42, p_satchel_scoped_fg42, p_satchel_mg34, p_satchel_mg42, p_satchel_panzerjager
new bool:g_round_restart = false
new bool:log_block_state = false
new gPlayerResetHUD[33] = { 1, ... }
new playerTNTCount[33], playerSATCHELCount[33]
new xplod, xplod2, shockwave, smoke, splash
static const decals[3] = {59, 60, 61} 

new fileNames[7][] =
{
	"models/tnt.mdl",
	"models/mapmodels/hk_satchel.mdl",
	"weapons/mortar_hit1.wav",
	"weapons/mortar_hit2.wav",
	"weapons/mortar_hit3.wav",
	"weapons/explodewater.wav",
	"object/object_dropped.wav"
}

enum
{
	tntmodel,
	satchelmodel,
	boomsound1,
	boomsound2,
	boomsound3,
	boomwater,
	dropsound
}

public plugin_init()
{
	register_plugin("DoD Explosives", "Beta1", "AMXX DoD Team")
	
	p_plugin = register_cvar("dod_explosives", "1")
	p_tntdelay = register_cvar("dod_explosives_tntdelay", "10")
	p_satcheldelay = register_cvar("dod_explosives_satcheldelay", "10")
	
	p_tnt_garand = register_cvar("dod_explosives_tnt_garand", "1")
	p_tnt_carbine = register_cvar("dod_explosives_tnt_carbine", "0")
	p_tnt_thompson = register_cvar("dod_explosives_tnt_thompson", "0")
	p_tnt_grease = register_cvar("dod_explosives_tnt_grease", "0")
	p_tnt_sniper = register_cvar("dod_explosives_tnt_sniper", "0")
	p_tnt_bar = register_cvar("dod_explosives_tnt_bar", "0")
	p_tnt_30cal = register_cvar("dod_explosives_tnt_30cal", "0")
	p_tnt_bazooka = register_cvar("dod_explosives_tnt_bazooka", "2")
	p_satchel_garand = register_cvar("dod_explosives_satchel_garand", "1")
	p_satchel_carbine = register_cvar("dod_explosives_satchel_carbine", "0")
	p_satchel_thompson = register_cvar("dod_explosives_satchel_thompson", "1")
	p_satchel_grease = register_cvar("dod_explosives_satchel_grease", "0")
	p_satchel_sniper = register_cvar("dod_explosives_satchel_sniper", "0")
	p_satchel_bar = register_cvar("dod_explosives_satchel_bar", "0")
	p_satchel_30cal = register_cvar("dod_explosives_satchel_30cal", "0")
	p_satchel_bazooka = register_cvar("dod_explosives_satchel_bazooka", "3")
	p_tnt_enfield = register_cvar("dod_explosives_tnt_enfield", "1")
	p_tnt_sten = register_cvar("dod_explosives_tnt_sten", "0")
	p_tnt_marksman = register_cvar("dod_explosives_tnt_marksman", "0")
	p_tnt_bren = register_cvar("dod_explosives_tnt_bren", "0")
	p_tnt_piat = register_cvar("dod_explosives_tnt_piat", "2")
	p_satchel_enfield = register_cvar("dod_explosives_satchel_enfield", "1")
	p_satchel_sten = register_cvar("dod_explosives_satchel_sten", "0")
	p_satchel_marksman = register_cvar("dod_explosives_satchel_marksman", "0")
	p_satchel_bren = register_cvar("dod_explosives_satchel_bren", "0")
	p_satchel_piat = register_cvar("dod_explosives_satchel_piat", "3")
	p_tnt_kar = register_cvar("dod_explosives_tnt_kar", "1")
	p_tnt_k43 = register_cvar("dod_explosives_tnt_k34", "0")
	p_tnt_mp40 = register_cvar("dod_explosives_tnt_mp40", "0")
	p_tnt_mp44 = register_cvar("dod_explosives_tnt_mp44", "0")
	p_tnt_scharfschutze = register_cvar("dod_explosives_tnt_scharfschutze", "0")
	p_tnt_fg42 = register_cvar("dod_explosives_tnt_fg42", "0")
	p_tnt_scoped_fg42 = register_cvar("dod_explosives_tnt_fg42scoped", "0")
	p_tnt_mg34 = register_cvar("dod_explosives_tnt_mg34", "0")
	p_tnt_mg42 = register_cvar("dod_explosives_tnt_mg42", "0")
	p_tnt_panzerjager = register_cvar("dod_explosives_tnt_panzerjager", "2")
	p_satchel_kar = register_cvar("dod_explosives_satchel_kar", "1")
	p_satchel_k43 = register_cvar("dod_explosives_satchel_k34", "0")
	p_satchel_mp40 = register_cvar("dod_explosives_satchel_mp40", "0")
	p_satchel_mp44 = register_cvar("dod_explosives_satchel_mp44", "0")
	p_satchel_scharfschutze = register_cvar("dod_explosives_satchel_scharfschutze", "0")
	p_satchel_fg42 = register_cvar("dod_explosives_satchel_fg42", "0")
	p_satchel_scoped_fg42 = register_cvar("dod_explosives_satchel_fg42scoped", "0")
	p_satchel_mg34 = register_cvar("dod_explosives_satchel_mg34", "0")
	p_satchel_mg42 = register_cvar("dod_explosives_satchel_mg42", "0")
	p_satchel_panzerjager = register_cvar("dod_explosives_satchel_panzerjager", "3")

	register_clcmd("plant_tnt", "funcPlantTNT")
	register_clcmd("plant_satchel", "funcPlantSATCHEL")
	
	register_event("ResetHUD", "resetHud", "be")
	register_event("RoundState", "roundMessage", "a", "1=1", "1=3", "1=4")
	register_forward(FM_AlertMessage, "log_block")
	register_message(get_user_msgid("DeathMsg"), "deathMessage")
}

public plugin_precache() 
{
	precache_model(fileNames[tntmodel])
	precache_model(fileNames[satchelmodel])
	precache_sound(fileNames[boomsound1])
	precache_sound(fileNames[boomsound2])
	precache_sound(fileNames[boomsound3])
	precache_sound(fileNames[boomwater])
	precache_sound(fileNames[dropsound])
	shockwave = precache_model("sprites/shockwave.spr")
	xplod = precache_model("sprites/cexplo.spr")
	xplod2 = precache_model("sprites/gexplo.spr")
	smoke = precache_model("sprites/puff.spr")
	splash = precache_model("sprites/wsplash3.spr")
}

public roundMessage() 
{
	if(!get_pcvar_num(p_plugin))
		return PLUGIN_CONTINUE 
		
	if(read_data(1) == 1)
	    g_round_restart = false
		
	if(read_data(1) == 3 || read_data(1) == 4)
		{
		g_round_restart = true

		new TNTid = engfunc(EngFunc_FindEntityByString, -1, "classname", "explosive_tnt")
		while((TNTid = engfunc(EngFunc_FindEntityByString, -1, "classname", "explosive_tnt")) > 0)
			{
			engfunc(EngFunc_RemoveEntity, TNTid)
			remove_task(TNTid)
			}
			
		new SATCHELid = engfunc(EngFunc_FindEntityByString, -1, "classname", "explosive_satchel")
		while((SATCHELid = engfunc(EngFunc_FindEntityByString, -1, "classname", "explosive_satchel")) > 0)
			{
			engfunc(EngFunc_RemoveEntity, SATCHELid)
			remove_task(SATCHELid)
			}
			
		new players[32], num
		get_players(players, num)
		
		for(new i = 0; i < num; i++)
			gPlayerResetHUD[players[i]] = 1
			
		}
	return PLUGIN_CONTINUE
}

public deathMessage(id)
{
	if(!get_pcvar_num(p_plugin))
		return PLUGIN_CONTINUE 
		
	gPlayerResetHUD[get_msg_arg_int(2)] = 1
	
	return PLUGIN_CONTINUE
}

public funcPlantTNT(id)
{
	if(get_pcvar_num(p_plugin) != 1 || !is_user_connected(id) || !is_user_alive(id) || !playerTNTCount[id] || g_round_restart)
		return PLUGIN_HANDLED
				
	new Float:origin[3]
	pev(id, pev_origin, origin)
	
	new TNTid = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
		
	if(!TNTid)
		return PLUGIN_HANDLED
			
	set_pev(TNTid, pev_classname, "explosive_tnt")
	engfunc(EngFunc_SetModel, TNTid, fileNames[tntmodel])
	set_pev(TNTid, pev_solid, SOLID_BBOX)
	set_pev(TNTid, pev_movetype, MOVETYPE_TOSS)
	set_pev(TNTid, pev_friction,0.85)	
	set_pev(TNTid, pev_gravity,0.75)
	set_pev(TNTid, pev_owner, id)
	set_pev(TNTid, pev_origin, origin)
	
	emit_sound(TNTid, CHAN_BODY, fileNames[dropsound], 1.0, ATTN_NORM, 0, PITCH_NORM)
	
	playerTNTCount[id]--
	client_print(id, print_chat, "You have %d TNT %s & %d Satchel %s Left...", playerTNTCount[id], (playerTNTCount[id] != 1) ? "Charges" : "Charge", playerSATCHELCount[id], (playerSATCHELCount[id] != 1) ? "Charges" : "Charge")	
		
	set_task(get_pcvar_float(p_tntdelay),"funcExplodeTNT",TNTid)

	return PLUGIN_HANDLED
}

public funcExplodeTNT(TNTid)
{
	if(!pev_valid(TNTid))
		return PLUGIN_CONTINUE

	new Float:origin[3], players[32], playercount
	pev(TNTid, pev_origin, origin)
		
	new iOrigin[3]
	iOrigin[0] = floatround(origin[0])
	iOrigin[1] = floatround(origin[1])
	iOrigin[2] = floatround(origin[2])
	
	if(engfunc(EngFunc_PointContents, origin) == CONTENTS_WATER)
		{
		emit_sound(TNTid, CHAN_BODY, fileNames[boomwater], 1.0, ATTN_NORM, 0, PITCH_NORM)
		
		message_begin(MSG_BROADCAST, SVC_TEMPENTITY, iOrigin)
		write_byte(17)
		write_coord(iOrigin[0])
		write_coord(iOrigin[1])
		write_coord(iOrigin[2]+15)
		write_short(splash)
		write_byte(15)
		write_byte(200)
		message_end()  
		}
	else
		{
		switch(random_num(1, 3))
			{
			case 1:	emit_sound(TNTid, CHAN_BODY, fileNames[boomsound1], 1.0, ATTN_NORM, 0, PITCH_NORM)
			case 2:	emit_sound(TNTid, CHAN_BODY, fileNames[boomsound2], 1.0, ATTN_NORM, 0, PITCH_NORM)
			case 3:	emit_sound(TNTid, CHAN_BODY, fileNames[boomsound3], 1.0, ATTN_NORM, 0, PITCH_NORM)
			}
		}
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY, iOrigin)
	write_byte(TE_EXPLOSION)
	write_coord(iOrigin[0])
	write_coord(iOrigin[1])
	write_coord(iOrigin[2] + 60)
	write_short(xplod)
	write_byte(22) 	//scale
	write_byte(14) 	//framerate
	write_byte(12) 	//flags
	message_end()
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY,iOrigin) 
	write_byte(TE_BEAMCYLINDER) 
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2] + 5) 
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2] + 150) 
	write_short(shockwave) 
	write_byte(0) //startframe 
	write_byte(0) //framerate 
	write_byte(3) //life
	write_byte(5) //width
	write_byte(100) //noise 
	write_byte(188) //r 
	write_byte(220) //g 
	write_byte(255) //b 
	write_byte(15) //brightness 
	write_byte(0) //speed 
	message_end() 
	
	message_begin( MSG_BROADCAST,SVC_TEMPENTITY,iOrigin) 
	write_byte(TE_SMOKE)
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2]) 
	write_short(smoke) 
	write_byte(random_num(10,20))
	write_byte(3)
	message_end()
	
	message_begin( MSG_BROADCAST,SVC_TEMPENTITY,iOrigin) 
	write_byte(TE_SMOKE)
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2]) 
	write_short(smoke) 
	write_byte(random_num(15,30))
	write_byte(4)
	message_end() 
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY) 
	write_byte(TE_WORLDDECAL)
	write_coord(floatround(origin[0]))
	write_coord(floatround(origin[1]))
	write_coord(floatround(origin[2]))
	write_byte(decals[random_num(0,2)]) //index 
	message_end() 
	
	get_players(players,playercount,"a")
		
	for(new i=0; i<playercount; i++)
		{
		new Float:playerOrigin[3], distance
		pev(players[i], pev_origin, playerOrigin)
		distance = floatround(get_distance_f(playerOrigin,origin))

		if(distance < 50)
			exp_fakedamage(players[i], TNTid,"TNT",200.0,DMG_BLAST)
		else if(distance < 100)
			exp_fakedamage(players[i], TNTid,"TNT",100.0,DMG_BLAST)
		else if(distance < 150)
			exp_fakedamage(players[i], TNTid,"TNT",75.0,DMG_BLAST)	
		else if(distance < 200)
			exp_fakedamage(players[i], TNTid,"TNT",40.0,DMG_BLAST)	
		else if(distance < 250)
			exp_fakedamage(players[i], TNTid,"TNT",20.0,DMG_BLAST)	
		else if(distance < 300)
			exp_fakedamage(players[i], TNTid,"TNT",10.0,DMG_BLAST)
		else if(distance < 350)
			exp_fakedamage(players[i], TNTid,"TNT",5.0,DMG_BLAST)	
			
		if(distance < 750)
			{
			message_begin(MSG_ONE_UNRELIABLE,get_user_msgid("ScreenShake"),{0,0,0},players[i])
			write_short(1<<14) //ammount 
			write_short(1<<12) //lasts this long 
			write_short(2<<14) //frequency
			message_end()
			}
		}
		
	engfunc(EngFunc_RemoveEntity, TNTid)
	
	return PLUGIN_CONTINUE
}

public funcPlantSATCHEL(id)
{
	if(get_pcvar_num(p_plugin) != 1 || !is_user_connected(id) || !is_user_alive(id) || !playerSATCHELCount[id] || g_round_restart)
		return PLUGIN_HANDLED
				
	new Float:origin[3]
	pev(id, pev_origin, origin)
	
	new SATCHELid = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
		
	if(!SATCHELid)
		return PLUGIN_HANDLED
			
	set_pev(SATCHELid, pev_classname, "explosive_satchel")
	engfunc(EngFunc_SetModel, SATCHELid, fileNames[satchelmodel])
	set_pev(SATCHELid, pev_solid, SOLID_BBOX)
	set_pev(SATCHELid, pev_movetype, MOVETYPE_TOSS)
	set_pev(SATCHELid, pev_friction,0.85)	
	set_pev(SATCHELid, pev_gravity,0.75)
	set_pev(SATCHELid, pev_owner, id)
	set_pev(SATCHELid, pev_origin, origin)
	
	emit_sound(SATCHELid, CHAN_BODY, fileNames[dropsound], 1.0, ATTN_NORM, 0, PITCH_NORM)
	
	playerSATCHELCount[id]--
	client_print(id, print_chat, "You have %d TNT %s & %d Satchel %s Left...", playerTNTCount[id], (playerTNTCount[id] != 1) ? "Charges" : "Charge", playerSATCHELCount[id], (playerSATCHELCount[id] != 1) ? "Charges" : "Charge")	
		
	
	set_task(get_pcvar_float(p_satcheldelay),"funcExplodeSATCHEL",SATCHELid)

	return PLUGIN_HANDLED
}

public funcExplodeSATCHEL(SATCHELid)
{
	if(!pev_valid(SATCHELid))
		return PLUGIN_CONTINUE
		
	new Float:origin[3], players[32], playercount
	pev(SATCHELid, pev_origin, origin)
		
	new iOrigin[3]
	iOrigin[0] = floatround(origin[0])
	iOrigin[1] = floatround(origin[1])
	iOrigin[2] = floatround(origin[2])
	
	if(engfunc(EngFunc_PointContents, origin) == CONTENTS_WATER)
		{
		emit_sound(SATCHELid, CHAN_BODY, fileNames[boomwater], 1.0, ATTN_NORM, 0, PITCH_NORM)
		
		message_begin(MSG_BROADCAST, SVC_TEMPENTITY, iOrigin)
		write_byte(17)
		write_coord(iOrigin[0])
		write_coord(iOrigin[1])
		write_coord(iOrigin[2]+15)
		write_short(splash)
		write_byte(25)
		write_byte(200)
		message_end()  
		}
	else
		{
		switch(random_num(1, 3))
			{
			case 1:	emit_sound(SATCHELid, CHAN_BODY, fileNames[boomsound1], 1.0, ATTN_NORM, 0, PITCH_NORM)
			case 2:	emit_sound(SATCHELid, CHAN_BODY, fileNames[boomsound2], 1.0, ATTN_NORM, 0, PITCH_NORM)
			case 3:	emit_sound(SATCHELid, CHAN_BODY, fileNames[boomsound3], 1.0, ATTN_NORM, 0, PITCH_NORM)
			}
		}
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY, iOrigin)
	write_byte(TE_EXPLOSION)
	write_coord(iOrigin[0])
	write_coord(iOrigin[1])
	write_coord(iOrigin[2] + 70)
	write_short(xplod2)
	write_byte(20) 	//scale
	write_byte(12) 	//framerate
	write_byte(12) 	//flags
	message_end()
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY,iOrigin) 
	write_byte(TE_BEAMCYLINDER) 
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2] + 5) 
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2] + 250) 
	write_short(shockwave) 
	write_byte(0) // startframe 
	write_byte(0) // framerate 
	write_byte(3) // life
	write_byte(7) // width
	write_byte(200) // noise 
	write_byte(188) // r 
	write_byte(220) // g 
	write_byte(255) // b 
	write_byte(15) //brightness 
	write_byte(0) // speed 
	message_end() 
	
	message_begin( MSG_BROADCAST,SVC_TEMPENTITY,iOrigin) 
	write_byte(TE_SMOKE)
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2]) 
	write_short(smoke) 
	write_byte(random_num(15,30))
	write_byte(3)
	message_end()
	
	message_begin( MSG_BROADCAST,SVC_TEMPENTITY,iOrigin) 
	write_byte(TE_SMOKE)
	write_coord(iOrigin[0]) 
	write_coord(iOrigin[1]) 
	write_coord(iOrigin[2]) 
	write_short(smoke) 
	write_byte(random_num(20,40))
	write_byte(4)
	message_end() 
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY) 
	write_byte(TE_WORLDDECAL)
	write_coord(floatround(origin[0]))
	write_coord(floatround(origin[1]))
	write_coord(floatround(origin[2]))
	write_byte(decals[random_num(0,2)]) // index 
	message_end() 
	
	get_players(players,playercount,"a")
		
	for(new i=0; i<playercount; i++)
		{
		new Float:playerOrigin[3], distance
		pev(players[i], pev_origin, playerOrigin)
		distance = floatround(get_distance_f(playerOrigin,origin))
		
		if(distance < 50)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",300.0,DMG_BLAST)
		else if(distance < 100)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",150.0,DMG_BLAST)
		else if(distance < 150)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",100.0,DMG_BLAST)	
		else if(distance < 200)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",75.0,DMG_BLAST)	
		else if(distance < 250)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",50.0,DMG_BLAST)	
		else if(distance < 325)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",25.0,DMG_BLAST)
		else if(distance < 400)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",15.0,DMG_BLAST)	
		else if(distance < 450)
			exp_fakedamage(players[i], SATCHELid,"SATCHEL",5.0,DMG_BLAST)	
			
		if(distance < 1000)
			{
			message_begin(MSG_ONE_UNRELIABLE,get_user_msgid("ScreenShake"),{0,0,0},players[i])
			write_short(1<<14) //ammount 
			write_short(1<<12) //lasts this long 
			write_short(2<<14) //frequency
			message_end()
			}
		}
		
	engfunc(EngFunc_RemoveEntity, SATCHELid)
	
	return PLUGIN_CONTINUE
}

public resetHud(id)
{
	if(!get_pcvar_num(p_plugin) || gPlayerResetHUD[id] == 0 || !is_user_alive(id) || !is_user_connected(id) || is_user_bot(id) || is_user_hltv(id))
		return PLUGIN_CONTINUE
	
	give_explosive(id)

	gPlayerResetHUD[id] = 0
	
	return PLUGIN_CONTINUE
}

give_explosive(id)
{
	if(get_pcvar_num(p_plugin) != 1 || !is_user_connected(id) || !is_user_alive(id) || is_user_bot(id) || is_user_hltv(id))
		return PLUGIN_HANDLED
	
	switch(dod_get_user_class(id))
		{
		//allies
		case DODC_GARAND: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_garand)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_garand)
				}
		case DODC_CARBINE: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_carbine)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_carbine)
				}
		case DODC_THOMPSON: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_thompson)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_thompson)
				}
		case DODC_GREASE: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_grease)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_grease)
				}
		case DODC_SNIPER: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_sniper)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_sniper)
				}
		case DODC_BAR: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_bar)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_bar)
				}
		case DODC_30CAL: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_30cal)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_30cal)
				}
		case DODC_BAZOOKA: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_bazooka)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_bazooka)
				}
		//brits tnt
		case DODC_ENFIELD: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_enfield) 
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_enfield) 
				}
		case DODC_STEN: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_sten) 
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_sten) 
				}
		case DODC_MARKSMAN: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_marksman) 
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_marksman) 
				}
		case DODC_BREN: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_bren) 
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_bren) 
				}
		case DODC_PIAT: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_piat)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_piat)
				}
		//axis tnt
		case DODC_KAR: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_kar)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_kar)
				}
		case DODC_K43: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_k43)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_k43)
				}
		case DODC_MP40: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_mp40)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_mp40)
				}
		case DODC_MP44: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_mp44)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_mp44) 
				}
		case DODC_SCHARFSCHUTZE: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_scharfschutze)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_scharfschutze) 
				}
		case DODC_FG42: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_fg42)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_fg42)
				}
		case DODC_SCOPED_FG42: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_scoped_fg42)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_scoped_fg42) 
				}
		case DODC_MG34: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_mg34)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_mg34) 
				}
		case DODC_MG42: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_mg42)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_mg42) 
				}
		case DODC_PANZERJAGER: {
				playerTNTCount[id] = get_pcvar_num(p_tnt_panzerjager)
				playerSATCHELCount[id] = get_pcvar_num(p_satchel_panzerjager)
				}
		}		
	return PLUGIN_CONTINUE
}

public log_block(type, msg[])
{
	return(log_block_state?FMRES_SUPERCEDE:FMRES_IGNORED)
}

stock exp_fakedamage(victim, explosive, const classname[], Float:takedmgdamage, damagetype)	//slighty modified from VEN's fakemeta_util
{
	new owner = pev(explosive, pev_owner)
	
	if(owner != victim && get_user_team(owner) == get_user_team(victim) && get_cvar_num("mp_friendlyfire") == 0)
		return 0
	
	if(get_user_health(victim) <= takedmgdamage)
		{	
		set_msg_block(get_user_msgid("DeathMsg"),BLOCK_ONCE)
		//set_msg_block(get_user_msgid("ScoreInfo"),BLOCK_ONCE)
		log_block_state = true
		user_kill(victim)
		log_block_state = false
		
		if(get_user_team(owner) != get_user_team(victim))
			dod_set_user_kills(owner, dod_get_user_kills(owner) + 1)
	
		message_begin(MSG_ALL, get_user_msgid("DeathMsg"),{500, 500, 500}, 0)
		write_byte(owner)
		write_byte(victim)
		write_byte(0)
		message_end()
		
		new classname[32]
		new steam[32], teamname[32], name[32]
		new steam2[32], teamname2[32], name2[32]
		get_user_authid(owner, steam, 31)
		get_user_authid(victim, steam2, 31)
		dod_get_pl_teamname(owner, teamname, 31)
		dod_get_pl_teamname(victim, teamname2, 31)
		get_user_name(owner, name, 31)
		get_user_name(victim, name2, 31)
		pev(explosive, pev_classname, classname, 31)
			
		if(owner == victim)
			log_message("^"%s<%d><%s><%s>^" committed suicide with ^"%s^"", name, get_user_userid(owner), steam, teamname, classname)
		else
			log_message("^"%s<%d><%s><%s>^" killed ^"%s<%d><%s><%s>^" with ^"%s^"", name, get_user_userid(owner), steam, teamname, name2, get_user_userid(victim), steam2, teamname2, classname)
		}	
	else
		{
		new entity = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "trigger_hurt"))
		
		if (!entity)
			return 0
	
		new value[16]
		format(value, 15, "%f", takedmgdamage * 2)
		fm_set_kvd(entity, "dmg", value, "trigger_hurt")
	
		format(value, 15, "%i", damagetype)
		fm_set_kvd(entity, "damagetype", value, "trigger_hurt")
		set_keyvalue(entity, "target", "blowingupnow")
		
		fm_set_kvd(entity, "origin", "8192 8192 8192", "trigger_hurt")
		dllfunc(DLLFunc_Spawn, entity)
			
		set_pev(entity,pev_classname,classname)
		dllfunc(DLLFunc_Touch,entity,victim)
		engfunc(EngFunc_RemoveEntity,entity)
		}
	return 1
}

stock fm_set_kvd(entity, const key[], const value[], const classname[] = "")	//from VEN's fakemeta_util
{
	if (classname[0])
		set_kvd(0, KV_ClassName, classname)
	else {
		new class[32]
		pev(entity, pev_classname, class, 31)
		set_kvd(0, KV_ClassName, class)
	}

	set_kvd(0, KV_KeyName, key)
	set_kvd(0, KV_Value, value)
	set_kvd(0, KV_fHandled, 0)

	return dllfunc(DLLFunc_KeyValue, entity, 0)
}

stock set_keyvalue(ent, key[], value[])
{
        new classname[32];
        pev(ent, pev_classname, classname, 31);
        set_kvd(0, KV_ClassName, classname);
        set_kvd(0, KV_KeyName, key);
        set_kvd(0, KV_Value, value);
        set_kvd(0, KV_fHandled, 0);
	 
        dllfunc(DLLFunc_KeyValue, ent, 0);
}
